package com.shop.api;

import com.shop.entity.AttributeResult;
import com.shop.entity.Category;
import com.shop.service.CategoryService;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/category")
public class CategoryApi {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/getCategories")
    public Result<List<AttributeResult>> getCategories() {
        List<AttributeResult> categories = categoryService.getCategories();

        if (CollectionUtils.isEmpty(categories)) {
            return ResultGenerator.genFailResult(50040);
        }
        return ResultGenerator.genSuccessResult(categories);
    }

    @GetMapping("/getCategoryAll")
    public Result getCategoryName() {
        List result = categoryService.getCategoryAllName();
        return ResultGenerator.genSuccessResult(result);


    }
    @GetMapping("/getCategoryName")
    public Result getCategoryName(@RequestParam Long categoryId) {
        if(categoryId<0)
            return ResultGenerator.genSuccessResult(null);
        String result = categoryService.getCategoryName(categoryId);
        HashMap<Long, String> a = new HashMap<>();
        a.put(categoryId,result);
        return ResultGenerator.genSuccessResult(a);

    }
}
